package nl.rainbowsheep.reisorg.rest.service.controller;

import java.util.List;
import nl.rainbowsheep.reisorg.rest.service.model.Region;
import nl.rainbowsheep.reisorg.rest.service.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.acls.model.NotFoundException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller for region
 */
@RestController
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * Method to get a json serialized Country object.
     *
     * @param name Name of the country
     * @return the saved country object
     * @throws NotFoundException when country with given name can't be found
     */
    @RequestMapping(value = "/getbyname", method = RequestMethod.GET)
    public Region getRegionByName(@RequestParam(value = "name", required = true) String name) throws NotFoundException {
        Region region = regionService.getRegionByName(name);
        if (region != null) {
            return region;
        } else {
            throw new NotFoundException(name);
        }
    }

    @RequestMapping(value = "/getregionbyid", method = RequestMethod.GET)
    @ResponseBody
    public Region getRegionById(@RequestParam(value = "id", required = true) Long id) throws Exception {
        Region region = regionService.getRegionById(id);
        return region;
    }
    
    @RequestMapping(value = "/getregion", method = RequestMethod.GET)
    @ResponseBody
    public List<Region> getById(@RequestParam(value = "id", required = true) Long id) throws Exception {
        List<Region> regions = regionService.getAllRegionsByCountryId(id);
        return regions;
    }
    
    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    public List<Region> getAllRegions() throws Exception {
        List<Region> regions = regionService.getAllRegions();
        return regions;
    }

    @RequestMapping(value = "/getbycountry", method = RequestMethod.GET)
    @ResponseBody
    public List<Region> getAllRegionsByCountry(@RequestParam(value = "country", required = true) String country) throws Exception {
        List<Region> regions = regionService.getAllRegionsByCountry(country);
        return regions;
    }

    @RequestMapping(value = "/getbycountryid", method = RequestMethod.GET)
    @ResponseBody
    public List<Region> getAllRegionsByCountryId(@RequestParam(value = "id", required = true) Long countryId) throws Exception {
        List<Region> regions = regionService.getAllRegionsByCountryId(countryId);
        return regions;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Region saveRegion(@RequestBody Region region) throws Exception {
        //TODO: Why exacly are we catching a thrown specific exception only to throw a generic exception???
        try {
            return regionService.save(region);
        } catch (Exception ex) {
            throw new Exception();
        }
    }

}
